package com.platform.core.service.impl;

import cn.hutool.core.map.MapUtil;
import com.platform.comm.dto.DtoMapper;
import com.platform.comm.service.impl.CommonBizImpl;
import com.platform.comm.util.StringUtil;
import com.platform.core.entity.CdComManageEntity;
import com.platform.core.entity.QCdComManageEntity;
import com.platform.core.repository.CdComManageRepository;
import com.platform.core.service.CdComManageBiz;
import com.platform.dto.CdComManageDto;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * Description: CdComManage-业务接口实现类
 *
 * @author Coder
 */
@Slf4j
@Service
public class CdComManageBizImpl extends CommonBizImpl<CdComManageRepository, CdComManageEntity, String> implements CdComManageBiz {

    private final QCdComManageEntity qCdComManage = QCdComManageEntity.cdComManageEntity;
    private final DtoMapper mapper;

    public CdComManageBizImpl(CdComManageRepository cdComManageRepository, DtoMapper mapper) {
        super(cdComManageRepository);
        this.mapper = mapper;
    }

    @Override
    public Page<CdComManageDto> search(Map<String,String> params, Pageable pageable){
        BooleanBuilder predicate = new BooleanBuilder();
        String comName = MapUtil.getStr(params, "comName");
        String tel = MapUtil.getStr(params, "tel");
        if (!StringUtil.isEmpty(comName)){
            predicate.and(qCdComManage.comName.contains(comName));
        }
        if (!StringUtil.isEmpty(tel)){
            predicate.and(qCdComManage.tel.contains(tel));
        }
        predicate.and(qCdComManage.flag.eq("1"));
        if (pageable.getSort().isUnsorted()) {
            pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
                    QSort.by(qCdComManage.sortNum.desc()));
        }
        Page<CdComManageEntity> page = queryByPage(predicate, pageable);
        return page.map(entity -> mapper.map(entity, CdComManageDto.class));
    }

    @Override
    public List<CdComManageDto> searchList() {
        List<CdComManageEntity> cdComManageEntities = queryAll(qCdComManage.flag.eq("1"));
        return cdComManageEntities.stream().map(entity -> mapper.map(entity, CdComManageDto.class)).collect(Collectors.toList());
    }

    @Override
    public CdComManageDto detail(String id) {
        return mapper.map(fetchById(id), CdComManageDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(CdComManageDto req) {
        CdComManageEntity entity = mapper.map(req, CdComManageEntity.class);
        entity.setFlag("1");
        saveEntity(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(CdComManageDto req) {
        CdComManageEntity entity = fetchById(req.getId());
        mapper.map(req, entity);
        saveEntity(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(String id){
        repository.deleteById(id);
    }
}
